/*
 * @Descripttion: 
 * @version: 
 * @Author: yuanliutech
 * @Date: 2022-11-02 14:26:03
 * @LastEditors: yuanliutech@lwj
 * @LastEditTime: 2023-02-14 19:54:06
 */
const app = getApp()
import Dialog from '@vant/weapp/dialog/dialog';
import {
    showMsg
} from '../../../../utils/config.js'
let spacePopoverSearch = require('../behaviors/space/space-popover-search.js')
import {
    reqIsolationPersonList,
    reqisolationPersonDel,
    reqisolationPersonRelieve,
    reqIsolationPersonStatistics
} from '../../../../request/api'
Page({
    behaviors: [spacePopoverSearch], //空间弹窗选择混入
    data: {
        navHeight: app.globalData.navHeight + 'px', //头部距离
        bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
        statistics: {
            inIsolation: 0,
            disIsolation: 0,
            todayIsolation: 0,
        },
        currentTab: 0, //隔离中/解除隔离
        tabList: [{
                id: 0,
                name: '隔离中'
            }, {
                id: 1,
                name: '解除隔离'
            },
            //  {
            //     id: 2,
            //     name: '待隔离'
            // },
        ],
        dataList: [],
        page: 1,
        searchValue: '',
        isNextPage: true,
        triggered: false,
        // 筛选弹窗开始
        showSearch: false, //筛选弹窗
        typeList: [{
            id: '',
            name: '全部',
            active: true,
        }, {
            id: 1,
            name: '居家隔离',
            active: false,
        }, {
            id: 2,
            name: '酒店隔离',
            active: false,
        }, {
            id: 3,
            name: '定点医院隔离',
            active: false,
        }, {
            id: 4,
            name: '其他隔离',
            active: false,
        }, ],
        isolationType: '', //选中的隔离类型
        hasSearch: false, //是否有筛选
        // 筛选弹窗结束
        // 更多操作开始
        showMoreActions: false, //是否展示更多操作
        listItemInfo: {}, //点击更多时列表一项信息
        // 更多操作结束
    },
    onLoad() {
        this.getBuildList()
    },
    onShow() {
        this.getStatistics()
        this.getInitDataList()
    },
    /**
     * @name:   点击切换 
     * @msg:    1、赋值current 
     * @param {*} e
     * @return {*}
     */
    swichNav(e) {
        let {
            current
        } = e.currentTarget.dataset
        this.setData({
            currentTab: current
        })
        this.getInitDataList()
    },
    /**
     * @name: 滑动切换
     * @msg:  1、赋值curren
     * @param {*} e
     * @return {*}
     */
    bindChangeTab(e) {
        let {
            current
        } = e.detail
        this.setData({
            currentTab: current
        })
        this.getInitDataList()
    },
    // 获取统计数据
    async getStatistics() {
        const res = await reqIsolationPersonStatistics(app.globalData.orgId)
        this.setData({
            statistics: res.data.row
        })
    },

    // 获取隔离人列表
    async getDataList() {
        let {
            currentTab
        } = this.data
        let isolationStatus = 2
        if (currentTab === 0) {
            isolationStatus = 2
        } else if (currentTab === 1) {
            isolationStatus = 3
        } else {
            isolationStatus = 1
        }
        let params = {
            orgId: app.globalData.orgId ==1?'':app.globalData.orgId,
            spaceId: this.data.spaceId,
            isolationStatus,
            name: this.data.searchValue,
            isolationType: this.data.isolationType,
        }
        const res = await reqIsolationPersonList(this.data.page, 10, params)
        if (res.code !== 20000) return showMsg(res.message)
        let {
            rows
        } = res.data
        if (this.data.page === 1) {
            this.setData({
                dataList: rows,
                isNextPage: true
            })
        } else if (this.data.page > 1 && rows.length) {
            this.setData({
                dataList: this.data.dataList.concat(rows),
                isNextPage: true
            })
        } else if (this.data.page > 1 && !rows.length) {
            showMsg('已经到底啦~')
            this.setData({
                isNextPage: false
            })
        }
    },
    // 滚动到底部触发
    lower() {
        if (!this.data.isNextPage) return
        this.setData({
            page: this.data.page + 1,
        })
        this.getDataList()
    },
    // 下拉刷新
    async onRefresh() {
        this.setData({
            dataList: [],
            page: 1,
            isNextPage: true,
            triggered: true,
        })
        await this.getDataList()
        this.setData({
            triggered: false,
        })
    },
    // 输入框搜索
    searchChange(e) {
        this.setData({
            searchValue: e.detail,
            page: 1,
            isNextPage: true
        })
        clearTimeout(this.data.timer)
        this.data.timer = setTimeout(() => {
            this.getDataList()
        }, 500)
    },
    // 操作列表后回到最初状态
    getInitDataList() {
        this.setData({
            page: 1,
            isNextPage: true,
            dataList: []
        })
        this.getDataList()
    },
    // 点击筛选
    clickSearch() {
        this.setData({
            showSearch: true
        })
    },
    // 点击关闭筛选
    closeSearch() {
        this.setData({
            showSearch: false
        })
    },
    // 点击隔离类型
    typeClick(e) {
        let {
            id
        } = e.currentTarget.dataset
        let {
            typeList
        } = this.data
        typeList.forEach(item => {
            if (item.id === id) {
                item.active = true
            } else {
                item.active = false
            }
        })
        this.setData({
            typeList,
            isolationType: id
        })
    },
    // 重置筛选
    resetSearch() {
        let {
            typeList,
            isolationType
        } = this.data
        typeList.forEach(item => {
            if (item.id === '') {
                item.active = true
            } else {
                item.active = false
            }
        })
        this.setData({
            typeList,
            isolationType: ''
        })
    },
    // 确定筛选点击
    confirmSearch() {
        let {
            isolationType
        } = this.data;
        let hasSearch = false;
        hasSearch = (isolationType) ? true : false
        this.setData({
            showSearch: false,
            hasSearch
        })
        this.getInitDataList()
    },


    // 点击更多操作
    clickMore(e) {
        let {
            item
        } = e.currentTarget.dataset
        this.setData({
            listItemInfo: item,
            showMoreActions: true
        })
    },
    // 点击xx
    closeMoreActions() {
        this.unShowMoreActions()
    },
    // 隐藏更多操作弹窗
    unShowMoreActions() {
        this.setData({
            showMoreActions: false
        })
    },

    /**
     * @name: 删除隔离
     * @msg: 
     * @return {*}
     */
    clickDelete() {
        let {
            id
        } = this.data.listItemInfo
        Dialog.confirm({
                title: '温馨提示',
                message: '此操作将删除该隔离人员，是否继续？',
            })
            .then(async () => {
                const res = await reqisolationPersonDel(id)
                if (res.code != 20000) return showMsg(res.message);
                this.unShowMoreActions()
                this.getInitDataList()
                this.getStatistics()
                showMsg("删除成功")
            })
            .catch(() => {});
    },

    // 解除隔离
    clickUnBind() {
        let {
            id
        } = this.data.listItemInfo
        Dialog.confirm({
                title: '温馨提示',
                message: '此操作将解除该人员隔离状态，是否继续？',
            })
            .then(async () => {
                const res = await reqisolationPersonRelieve(id)
                if (res.code != 20000) return showMsg(res.message);
                this.unShowMoreActions()
                this.getInitDataList()
                showMsg("解除成功")
            })
            .catch(() => {});
    },
    // 点击删除已解除隔离人员
    clickUnbindDelete(e) {
        let {
            id
        } = e.currentTarget.dataset
        Dialog.confirm({
                title: '温馨提示',
                message: '此操作将删除该隔离人员，是否继续？',
            })
            .then(async () => {
                const res = await reqisolationPersonDel(id)
                if (res.code != 20000) return showMsg(res.message);
                this.getInitDataList()
                showMsg("删除成功")
            })
            .catch(() => {});
    },
    // 去新增隔离人员
    goAddDevice() {
        // 如果有建筑物筛选且筛选到安装点，则新增时给一个快捷安装点
        let {
            roomId,
            spaceName
        } = this.data
        let params = {
            spaceId: roomId,
            spaceName
        }
        let info = JSON.stringify(params)
        wx.navigateTo({
            url: `/packageA/pages/index-inside-page/isolation-add/isolation-add?info=${info}`,
        })
    },
    // 去隔离详情
    goIsolationPersonDetail(e) {
        let {
            item
        } = e.currentTarget.dataset
        let info = JSON.stringify(item)
        wx.navigateTo({
            url: `/pages/index-inside-page/isolation-detail/isolation-detail?info=${info}`,
        })
    },

    // 点击拨打电话
    clickCall(e) {
        let {
            mobile
        } = e.currentTarget.dataset
        if (!mobile) return
        Dialog.confirm({
                title: '温馨提示',
                message: `是否拨打${mobile}号码?`,
            })
            .then(async () => {
                wx.makePhoneCall({
                    phoneNumber: mobile,
                })
            })
            .catch(() => {});
    },
})